package com.itheima.listener;

import com.itheima.domain.mongo.MovementScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-recommend-movement",consumerGroup = "recommendMovement")
public class MovementScoreListener implements RocketMQListener<Map> {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     *  map 谁 对什么内容进行了 什么操作
     */
    @Override
    public void onMessage(Map map) {
        Long userId = Long.valueOf(map.get("uid").toString());//谁
        Long pid = Long.valueOf(map.get("pid").toString());// 对什么内容
        Integer type = Integer.valueOf(map.get("type").toString());//什么操作
        //创建动态的评分信息表
        MovementScore movementScore = new MovementScore();
        movementScore.setUserId(userId);//哪个用户
        movementScore.setPublishId(pid);//什么内容
        movementScore.setDate(System.currentTimeMillis());//时间戳
        switch (type) { //以后得分的计算规则, 按照真实开发需求文档进行计算
            case 1: {
                movementScore.setScore(20d);
                break;
            }
            case 2: {
                movementScore.setScore(1d);
                break;
            }
            case 3: {
                movementScore.setScore(5d);
                break;
            }
            case 4: {
                movementScore.setScore(8d);
                break;
            }
            case 5: {
                movementScore.setScore(10d);
                break;
            }
            case 6: {
                movementScore.setScore(-5d);
                break;
            }
            case 7: {
                movementScore.setScore(-8d);
                break;
            }
        }
        mongoTemplate.save(movementScore);


    }
}
